﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunWMS.SpiService;

internal class BBulidPlanImpl : IBulidPlanService
{
    private readonly static IWcsBulidPlanAsrs _WcsBulidPlanAsrs = new BWcsBulidPlanAsrs();
    private readonly static IWcsScanner _WcsScanner = new BWcsScanner();
    public List<BulidPlanBean> GetPlanListByDate(BulidPlanParam param)
    {
        var date = param.Date.Trim();
        var mesPlans = _WcsBulidPlanAsrs.GetPlanListByDate(param.Date, param.Type);

        //WriteLog($"获取成型表内数据{mesPlans.ToJson()}");
        DateTime startTime = new();
        DateTime endTime = new();

        switch (param.Type)
        {
            case "01":
                startTime = Convert.ToDateTime($"{date} 00:30:00");
                endTime = Convert.ToDateTime($"{date} 08:30:00");
                break;
            case "02":
                startTime = Convert.ToDateTime($"{date} 08:30:00");
                endTime = Convert.ToDateTime($"{date} 16:30:00");
                break;
            case "03":
                startTime = Convert.ToDateTime($"{date} 16:30:00");
                endTime = Convert.ToDateTime($"{date} 00:30:00");
                break;
        }

        var scannerMaterial = _WcsScanner.GetScannerMaterialGroupByDate(startTime, endTime);
        //WriteLog($"获取成型扫码数据{mesPlans.ToJson()}");
        var result = scannerMaterial.GroupJoin(mesPlans, f => f.MaterialCode, s => s.ProductionNoS, (f, s) => new
        {
            ScannerMaterial = f,
            MesPlans = s,
        }).SelectMany(t => t.MesPlans.DefaultIfEmpty(), (f, s) => new BulidPlanBean
        {
            RealCount = f.ScannerMaterial.Count,
            PlanCount = s?.Qty ?? 0,
            MaterialCode=f.ScannerMaterial.MaterialCode,
        }).ToList();
        return result;


        //用扫码记录  然后用生产时间去匹配???
        //_BakWcsTask.GetMaterialGroupByDateAndType((int)enum)

        //_WcsScanner.get
        return null;
    }
}

